<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/13
 * Time: 下午8:25
 */
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class AdminController extends AdminBaseController{
    //展示管理员列表附带搜索功能
    public function index(){
        $inquery = I('get.inquery/s');
        $page = I('get.page/d',1);
        $admin = M('admin');
        $limit = 5;
        $map = empty($inquery)?'':'username LIKE "%'. $inquery .'%" or realName like "%' . $inquery . '%"';
        $count=$admin->where($map)->count();
//        $page=new \Org\Page($count,$limit,['inquery' => $inquery]);
//        $list=$admin->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
//        $show=$page->show();
//        $this -> assign('list',$list);
//        $this->assign('page',$show);
        $pages =ceil($count/$limit);
        $pageNum = ($page -1) * $limit;
        $list=$admin -> field('dx_admin.id,username,litpic,dx_admin.status,realName,loginNum,ip,loginTime,dx_group.title') -> join('LEFT JOIN __ADMIN_GROUP__ ON __ADMIN__.id = __ADMIN_GROUP__.uid')-> join('LEFT JOIN __GROUP__ ON __ADMIN_GROUP__.group_id = __GROUP__.id') -> where($map)->order('id desc')->limit($pageNum,$limit)->select();
        $this -> assign('pages',$pages);
        $this -> assign('list',$list);
        $this -> assign('inquery',$inquery);
        $this -> display();
    }

    //删除管理员
    public function adminDel(){
        if(IS_POST){
            $id = I('post.id/d');
            if(($id != 1) && !empty($id)){
                $admin = M('admin');
                $admin ->startTrans();
                $map['id'] = $id;
                $result = $admin -> where($map) -> delete();
                $group = M('admin_group');
                $where['uid'] = $id;
                $result2 = $group ->where($where) ->delete();
                if(empty($result) && empty($result2)){
                   $admin -> rollback();
                }else{
                    $admin -> commit();
                }
                $this->ajaxReturn([ 'title' => '删除成功！','msg' => '您已经永久删除了这条信息。','status' => 'success']);
            }else{
                $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
            }
        }else{
            $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
        }
    }

    //改变用户状态
    public function adminChange(){
        if(IS_POST){
            $id = I('post.id/d');
            $admin = M('admin');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $data = $admin -> field('status') -> where($map) -> find();
                if(!empty($data)){
                    if($data['status'] == 1 ){
                        $change['status'] = 0;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '禁用成功！','status' => 1,'state' => 0]);
                    }else{
                        $change['status'] = 1;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '启用成功！','status' => 1,'state' => 1]);
                    }
                }else{
                    $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
                }
            }else{
                $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
            }

        }else{
            $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
        }
    }
    public function add(){
        $group = M('group');
        $groupInfo = $group -> field('id,title') -> select();
        $this->assign('list',$groupInfo);
        $this ->display();
    }
    public function save(){
        $id = I('post.id/d',0);
        if(empty($id)){
            if(IS_POST){
                $username = I('post.username/s');
                $realName = I('post.realName/s');
                $password = I('post.password/s');
                $confirm_password = I('post.confirm_password/s');
                $status = I('post.status/d');
                $title = I('post.title/d');
                $fileData = $_FILES['litpic'];
                if(empty($fileData)){
                    $this->ajaxReturn(['msg' => '图片上传失败！','status' => '0']);
                }
                if($fileData['size'] > 204800){
                    $this->ajaxReturn(['msg' => '图片太大！','status' => '0']);
                }
                $datatype = ["image/png","image/jpg","image/jpeg","image/gif"];
                if(!in_array($fileData['type'],$datatype)){
                    $this->ajaxReturn(['msg' => '上传图片类型错误!','status' => '0']);
                }
                $fileName = time().'.'.end(explode('.',$fileData['name']));
                $filePath = 'Upload/head/';
                if(!is_dir($filePath) && !is_writable($filePath)){
                    $this->ajaxReturn(['msg' => '文件夹存储异常！','status' => 0]);
                }
                $litPath = $filePath.$fileName;
                if(!move_uploaded_file($fileData['tmp_name'],$litPath)){
                    $this->ajaxReturn([ 'msg' => '头像上传失败！','status' => 0]);
                }
                if((empty($username)) && (empty($realName)) && (empty($password)) && (empty($confirm_password)) && (empty($status)) && (empty($title))){
                    $this->ajaxReturn(['msg' => '发生错误，请重新填写!','status' => '0']);
                }
                $admin = M('admin');
                $adminInfo =$admin -> where(['username' => $username])->find();
                if($adminInfo){
                    $this->ajaxReturn(['msg' => '该用户已存在！','status' => '0']);
                }
                if($password != $confirm_password){
                    $this->ajaxReturn(['msg' => '两次密码不同！请重新填写~','status' => '0']);
                }
                $data['username'] = $username;
                $data['realName'] = $realName;
                $data['password'] = _md5($password);
                $data['status'] = $status;
                $data['litpic'] = $litPath;
                $adminGroup = M('admin_group');
                $admin -> startTrans();
                $result = $admin -> data($data) -> add();
                $adminInfo2 = $admin -> field('id') ->where(['username' => $username]) -> find();
                $map['uid'] = $adminInfo2['id'];
                $map['group_id'] = $title;
                $result2 = $adminGroup -> data($map) -> add();
                if(!empty($result) && !empty($result2)){
                    $admin -> commit();
                    $this->ajaxReturn(['msg' => '添加成功！','status' => '1']);
                }else{
                    $admin -> rollback();
                    $this->ajaxReturn(['msg' => '添加失败!','status' => '0']);
                }
            }else{
                $this->ajaxReturn(['msg' => '添加失败!','status' => '0']);
            }
        }else{
            if(IS_POST){
                $username = I('post.username/s');
                $realName = I('post.realName/s');
                $title = I('post.title/d');
                $admin = M('admin');
                $adminGroup = M('admin_group');
                $admin ->startTrans();
                $data['username'] =$username;
                $data['realName'] =$realName;
                $result = $admin ->where(['id' => $id]) -> save($data);
                $map['group_id'] = $title;
                $result2 = $adminGroup -> where(['uid' => $id]) -> save($map);
                if(isset($result) && isset($result2)){
                    $admin -> commit();
                    $this->ajaxReturn(['msg' => '修改成功！','status' => '1']);
                }
                $admin ->rollback();
                $this->ajaxReturn(['msg' => '修改失败!','status' => '0']);
            }else{
                $this->ajaxReturn(['msg' => '修改失败!','status' => '0']);
            }
        }
    }
    public function edit(){
        if(IS_GET){
            $id = I('get.id/d',0);
            if(empty($id)){
                $this -> redirect('Admin/Admin/index');
            }
            $admin = M('admin');
            $adminInfo = $admin-> field('dx_admin.id,username,realName,group_id,litpic') -> join('LEFT JOIN __ADMIN_GROUP__ ON __ADMIN__.id = __ADMIN_GROUP__.uid')-> where(['id' => $id]) ->find();
            $this -> assign('adminInfo',$adminInfo);
            $group = M('group');
            $groupInfo = $group -> field('id,title') -> select();
            $this->assign('list',$groupInfo);
            $this->display();
        }
    }
}